//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.erp.filter;

import com.example.erp.log.LoggerAppend;
import com.example.erp.log.LoggerType;
import com.example.erp.utils.CommonUtils;
import com.example.erp.utils.TraceUtils;
import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.core.SpringProperties;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.InetAddress;

/**
 * @author 18481
 */
@WebFilter(urlPatterns = {"/*"}, filterName = "webFilter")
public class WebAccessFilter implements Filter {

    private String encoding_utf8 = "UTF-8";
    public static final Logger LOGGER = LoggerFactory.getLogger(WebAccessFilter.class);

    public WebAccessFilter() {
    }

    public void init(FilterConfig filterConfig) {
        if (TraceUtils.isSupportTraced()) {
            ApplicationContext context = (ApplicationContext) filterConfig.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            TraceUtils.setApplicationContext(context);
        }

        try {
            InetAddress addr = InetAddress.getLocalHost();
            System.setProperty("sIP", addr.getHostAddress());
            SpringProperties.setProperty("sIP",addr.getHostAddress());
        } catch (Exception var3) {
            LOGGER.error("获取本地ip地址失败：" + var3.getMessage(), var3);
        }

    }

    private void requestInit(HttpServletRequest request) {
        String ip = CommonUtils.getIPAddress(request);
        String traceId = TraceUtils.getTraceId(request);
        ThreadContext.put("uIP", ip);
        ThreadContext.put("traceID", traceId);
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long start = System.currentTimeMillis();
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        this.requestInit(httpRequest);
        boolean var25 = false;

        try {
            var25 = true;
            response.setCharacterEncoding(this.encoding_utf8);
            chain.doFilter(request, response);
            var25 = false;
        } finally {
            if (var25) {
                try {
                    long useTime = System.currentTimeMillis() - start;
                    LoggerAppend append = LoggerAppend.create(httpRequest, LoggerType.WEB_ACCESS);
                    this.appendError(append, request);
                    append.append("costTime", useTime);
                    LOGGER.info(append.toString());
                } finally {
                    ThreadContext.clearAll();
                }
            }
        }

        try {
            long useTime = System.currentTimeMillis() - start;
            LoggerAppend append = LoggerAppend.create(httpRequest, LoggerType.WEB_ACCESS);
            this.appendError(append, request);
            append.append("costTime", useTime);
            LOGGER.info(append.toString());
        } finally {
            ThreadContext.clearAll();
        }

    }

    public void destroy() {
    }

    private void appendError(LoggerAppend append, ServletRequest request) {
        if (request.getAttribute("errorCode") != null) {
            String code = String.valueOf(request.getAttribute("errorCode"));
            String reason = String.valueOf(request.getAttribute("errorReason"));
            append.append("errorCode", code);
            append.append("errorReason", reason);
        }

    }
}
